package com.benzhuo.widget.imageselector.multi_image_selector;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.benzhuo.widget.imageselector.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/15.
 */
public class PhotoPickerActivity extends Activity implements MultiImageSelectorFragment.Callback, View.OnClickListener {
    public static final String RESULT_SELECTED_PICTURE = "selectedPicture";

    public static final String EXTRA_SELECT_COUNT = "max_select_count";
    public static final String EXTRA_SHOW_CAMERA = "show_camera";
    public static final String EXTRA_DEFAULT_SELECTED_LIST = "default_list";



    MultiImageSelectorFragment fragment;
    private Button countV;
    private int maxCount = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mis_activity_photo_picker);
        countV = (Button)findViewById(R.id.countV);
        countV.setOnClickListener(this);

        maxCount = getIntent().getIntExtra(EXTRA_SELECT_COUNT,1);
        boolean showCamera =  getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA,true);
        ArrayList<String> selected = getIntent().getStringArrayListExtra(EXTRA_DEFAULT_SELECTED_LIST);

        Bundle bundle = new Bundle();
        bundle.putInt(MultiImageSelectorFragment.EXTRA_SELECT_COUNT, maxCount);
        bundle.putInt(MultiImageSelectorFragment.EXTRA_SELECT_MODE, maxCount>1?MultiImageSelectorFragment.MODE_MULTI:MultiImageSelectorFragment.MODE_SINGLE);
        bundle.putBoolean(MultiImageSelectorFragment.EXTRA_SHOW_CAMERA, showCamera);
        if (selected!=null&&!selected.isEmpty()){
            bundle.putStringArrayList(MultiImageSelectorFragment.EXTRA_DEFAULT_SELECTED_LIST,selected);
        }
        if (maxCount>1){
            countV.setVisibility(View.VISIBLE);
            int sel = selected==null?0:selected.size();
            countV.setText(""+sel+"/"+maxCount);
        }else {
            countV.setVisibility(View.GONE);
        }
        // Add fragment to your Activity

        fragment = (MultiImageSelectorFragment) Fragment.instantiate(this, MultiImageSelectorFragment.class.getName(), bundle);
        getFragmentManager().beginTransaction()
                .add(R.id.container, fragment)
                .commit();

        findViewById(R.id.navBackV).setOnClickListener(this);
    }

    @Override
    public void onSingleImageSelected(String path) {
        selectBack();
    }

    @Override
    public void onImageSelected(String path) {
        countV.setText(""+fragment.selectedCount()+"/"+maxCount);
    }

    @Override
    public void onImageUnselected(String path) {
        countV.setText(""+fragment.selectedCount()+"/"+maxCount);
    }

    @Override
    public void onCameraShot(File imageFile) {
        selectBack();
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.navBackV){
            finish();
        }else if (view.getId()==R.id.countV){
            selectBack();
        }
    }

    private void selectBack(){
        ArrayList<String> temp=fragment.getResultList();
        Intent intent = new Intent();
        intent.putExtra(RESULT_SELECTED_PICTURE,temp);
        setResult(RESULT_OK,intent);
        finish();
    }

}
